package logic

import (
	"context"
	"fmt"
	"product/model/mysql"

	"product/goodrpc/goodrpc"
	"product/goodrpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddLogic {
	return &AddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AddLogic) Add(in *goodrpc.AddRequest) (*goodrpc.AddResponse, error) {
	// todo: add your logic here and delete this line
	var good = mysql.Good{
		Userid:   int(in.Userid),
		Name:     in.Name,
		Image:    in.Image,
		Quantity: int(in.Quantity),
		Price:    float64(in.Price),
	}
	err := mysql.DB.Debug().Model(&mysql.Good{}).Create(&good).Error
	if err != nil {
		return nil, fmt.Errorf("添加商品失败")
	}
	return &goodrpc.AddResponse{Id: int64(good.ID)}, nil
}
